package com.edu.admin.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysSchoolRoleMenu;
import com.edu.admin.mapper.SchoolRoleMenuMapper;
import com.edu.admin.vo.SchoolRoleMenuVo;

/**
 * 学校角色菜单服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class SchoolRoleMenuService extends BaseService<SchoolRoleMenuMapper, SysSchoolRoleMenu> {
	
	/**
	 * 获取学校角色菜单
	 * 
	 * @param schoolRoleId
	 * @param menuId
	 * 
	 * @return SysSchoolRoleMenu
	 */
	public SysSchoolRoleMenu getSchoolRoleMenuBySchoolRoleIdAndMenuId(Long schoolRoleId,Long menuId){
		return this.mapper.getSchoolRoleMenuBySchoolRoleIdAndMenuId(schoolRoleId,menuId);
	}
	/**
	 * 按菜单id获取学校角色菜单列表
	 * 
	 * @param menuId
	 * 
	 * @return List<SchoolRoleMenuVo>
	 */
	public List<SchoolRoleMenuVo> getSchoolRoleMenuListByMenuId(Long menuId) {
		return this.mapper.getSchoolRoleMenuListByMenuId(menuId);
	}
	
	/**
	 * 按校级角色获取学校角色菜单列表
	 * 
	 * @param schoolRoleId
	 * @return List<SchoolRoleMenuVo>
	 */
	public List<SchoolRoleMenuVo> getSchoolRoleMenuListBySchoolRoleId(Long schoolRoleId) {
		return this.mapper.getSchoolRoleMenuListBySchoolRoleId(schoolRoleId);
	}
	
	/**
	 * 删除学校角色下菜单
	 * 
	 * @param schoolRoleId
	 * 
	 * @return
	 */
	public void deleteSchoolRoleMenuBySchoolRoleId(Long schoolRoleId) {
		this.mapper.deleteSchoolRoleMenuBySchoolRoleId(schoolRoleId);
	}
	
	
	/**
	 * 学校角色菜单保存
	 * 
	 * @param schoolRoleId
	 * @param menuIds
	 * 
	 * @return
	 */
	public void saveSchoolRoleMenu(Long schoolRoleId, Integer[] menuIds) {
		
		SysSchoolRoleMenu schoolRoleMenu = null;
		
		deleteSchoolRoleMenuBySchoolRoleId(schoolRoleId);
		
		for (int i=0;i<menuIds.length;i++)
		{
			schoolRoleMenu = new SysSchoolRoleMenu();
			schoolRoleMenu.setSchoolRoleId(schoolRoleId);
			schoolRoleMenu.setMenuId(menuIds[i].longValue());
			
			insert(schoolRoleMenu);
		}

	}
	
	/**
	 * 获取角色所有权限URL
	 * 
	 * @param schoolRoleId
	 * @return String
	 */
	public String getUrlsBySchoolRoleId(Long schoolRoleId) {
		if (schoolRoleId==null) {
			return "";
		}
		
		SchoolRoleMenuVo schoolRoleMenu = this.mapper.getUrlsBySchoolRoleId(schoolRoleId);
		if (schoolRoleMenu!=null)
		{
			return schoolRoleMenu.getRequestUrls();
		}
		return "";
	}
	

}
